package com.yyairmarkyy.arctool.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;


import com.yyairmarkyy.arctool.inter.ArcOutsideClickLisitener;
import com.yyairmarkyy.arctool.item.ArcFadeItem;
import com.yyairmarkyy.arctool.util.Utils;

import java.util.List;


/**
 * Created by yyairmarkyy on 2017/10/18.
 */

public class ArcToolRootLyt extends FrameLayout {


    private Paint bgPaint;
    private Path mPath;

    private int bgRadius;
    private int bgAlpha;
    private int bgColor;
    private List<ArcFadeItem> fadeItems;
    private ArcOutsideClickLisitener outsideClickLisitener;

    public ArcToolRootLyt(@NonNull Context context) {
        super(context);
        init();
    }

    public ArcToolRootLyt(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);



    }

    public ArcToolRootLyt(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void init(){
        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setDither(true);
        bgPaint.setStyle(Paint.Style.FILL);
        mPath = new Path();
    }


    /**
     * 设置背景半径
     * @param bgRadius
     */
    public void setBgRadius(int bgRadius) {
        this.bgRadius = bgRadius;
    }

    /**
     * 设置背景透明度 0-255
     * @param bgAlpha
     */
    public void setBgAlpha(int bgAlpha) {
        this.bgAlpha = bgAlpha;
        bgPaint.setColor(bgColor);
        bgPaint.setAlpha(bgAlpha);
    }

    /**
     * 设置背景颜色
     * @param bgColor
     */
    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        bgPaint.setColor(bgColor);
    }

    /**
     * 设置分割线
     * @param fadeItems
     */
    public void setFadeItems(List<ArcFadeItem> fadeItems) {
        this.fadeItems = fadeItems;
    }


    public void setOutsideClickLisitener(ArcOutsideClickLisitener outsideClickLisitener) {
        this.outsideClickLisitener = outsideClickLisitener;
    }

    public void removeOutsideClickLisitener(){
        if (outsideClickLisitener != null)
            outsideClickLisitener = null;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mPath.reset();
        mPath.addCircle(getWidth(),getHeight(),bgRadius, Path.Direction.CW);
        if (ev.getAction() == MotionEvent.ACTION_DOWN && !Utils.pointInPath(mPath,new Point((int)ev.getRawX(),(int)ev.getRawY()))) {
           if (outsideClickLisitener != null)
               outsideClickLisitener.onClickOutside();
        }

        return super.dispatchTouchEvent(ev);
    }

    /**
     * 绘画分割线
     * @param canvas
     */
    private void drawFades(Canvas canvas){
        if (fadeItems != null && fadeItems.size() > 0){
            for (ArcFadeItem fadeItem : fadeItems){

                if (fadeItem.getVisiable() == ArcFadeItem.GONE)
                    continue;

                bgPaint.setStyle(Paint.Style.STROKE);
                bgPaint.setColor(fadeItem.getBackgroudColor());
                bgPaint.setAlpha(fadeItem.getAlpha());
                // TODO: 2017/10/23 实现全透明效果
                bgPaint.setStrokeWidth(fadeItem.getFadeWith());
                if (fadeItem.getAlpha() == 0) {
                    bgPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
                    bgPaint.setAlpha(255);
                }
                canvas.drawCircle(getWidth(),getHeight(),fadeItem.getRadius(),bgPaint);
            }
        }
    }

    private void initBgpatint(){
        if (bgPaint == null)
            return;
        bgPaint.setAntiAlias(true);
        bgPaint.setDither(true);
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(bgColor);
        bgPaint.setAlpha(bgAlpha);
        bgPaint.setXfermode(null);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.drawColor(0xb2000000);
        initBgpatint();
       RectF rectF = new RectF(getWidth()-bgRadius,getHeight()-bgRadius,getWidth(),getHeight());
        int sc = canvas.saveLayer(rectF, bgPaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawCircle(getWidth(),getHeight(),bgRadius, bgPaint);
        drawFades(canvas);
        canvas.restoreToCount(sc);
        super.dispatchDraw(canvas);
    }

}
